package com.lab.dao.impl;


import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.lab.po.User;
import com.lab.util.MySqlSessionFactory;
import com.lab.dao.UserDao;


//数据库访问层-管理员的CRUD操作
public class UserDaoImpl implements UserDao {
	//用户登录验证
	  public boolean isLogin(User user){
		  boolean flag=false;
		  SqlSession session=null;
		  try {
			session=MySqlSessionFactory.getSqlSession();
			Map<String,String> map=new HashMap<String,String>();
			map.put("username", user.getUsername());//对传过来的用户名和密码进行封装װ
			map.put("password", user.getPassword());
			int i=session.selectOne("UserXml.isLogin",map);
			session.commit();
			if(i==1){
				flag=true;				
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return flag;	  
	  }
		 //修改管理员的密码
		public int updateUserPassWord(User  user) {
			int a=0;
			SqlSession session=null;
			try {
				session=MySqlSessionFactory.getSqlSession();
				Map<String,String> map=new HashMap<String,String>();
				map.put("password", user.getPassword());
				map.put("username", user.getUsername());
				a=session.update("UserXml.updateUserPassWord",map);
				session.commit();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			return a;
		}
		
		//学生登录验证
		@Override
		public boolean stuLogin(User user) {
			boolean flag=false;
			  SqlSession session=null;
			  try {
				session=MySqlSessionFactory.getSqlSession();
				Map<String,String> map=new HashMap<String,String>();
				map.put("stId", user.getUsername());//对传过来的用户名和密码进行封装װ
				map.put("pwd", user.getPassword());
				int i=session.selectOne("PassWordXml.stuLogin",map);
				session.commit();
				if(i==1){
					flag=true;				
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				if(session!=null){
					session.close();
				}
			}
			return flag;
		}
		
		//修改学生密码
		@Override
		public int updateStuPwd(Map map) {
			int a=0;
			SqlSession session=null;
			try {
				session=MySqlSessionFactory.getSqlSession();
				a=session.update("UserXml.updateStuPwd",map);
				session.commit();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			return a;
		}
}
